//
//  MusicViewController.m
//  播求
//
//  Created by MS on 15-9-21.
//  Copyright (c) 2015年 gengsensen. All rights reserved.
//

#import "MusicViewController.h"
#import "MusicTableViewCell.h"
#import "JokeingModel.h"
#import "FSAudioStream.h"

@interface MusicViewController ()<TermCellDelegate,AVAudioSessionDelegate>
@property (nonatomic,strong) FSAudioStream *audioStream;

@end

@implementation MusicViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem * BarButton = [[UIBarButtonItem alloc]initWithTitle:@"刷新" style:UIBarButtonItemStylePlain target:self action:@selector(TapBarButton:)];
    
    self.navigationItem.rightBarButtonItem =  BarButton;
    
    
    }
-(void)viewWillAppear:(BOOL)animated{
    
    
    [DownLoadData getRequestType:Enum_MusicPage withData:^(id obj, NSError *err) {
        
        if(obj)
        {
            
            self.dataArr=obj;
            
            [self.tableView reloadData];
            [self.tableView.footer endRefreshing];
        }
        else
        {
            UIAlertView * a = [[UIAlertView alloc] initWithTitle:@"提示" message:@"数据下载失败" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [a show];
            NSLog(@"段子---数据下载失败");
            a = nil;
        }
        
        
    } withPage:0];
    
    
}

#pragma mark-----UITableViewDataSource,UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    static NSString * string = @"cellID";
    MusicTableViewCell * cell =[self.tableView dequeueReusableCellWithIdentifier:string];
    if (cell == nil) {
        cell = [[MusicTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    }
    
    
    cell.delegate = self;
    JokeingModel * model = self.dataArr[indexPath.row];
    
    [cell.personImageView setImageWithURL:[NSURL URLWithString:model.profile_image]];
    cell.nameLable.text = model.name;
    cell.nameLable.font = [UIFont systemFontOfSize:13];
    
    cell.dateLabel.text = model.create_time;
    cell.dateLabel.font = [UIFont systemFontOfSize:10];
    
    cell.loveLabel.text = model.love;
    cell.hateLabel.text = model.hate;
    
    cell.TextLabel.text = model.Mytext;
    
    [cell.ImageButton setImage:[UIImage imageNamed:@"u=1205398626,1401701630&fm=21&gp=0.jpg"] forState:UIControlStateNormal];
    
    [cell.PictureImageView setImageWithURL:[NSURL URLWithString:model.bimageuri]];
   

    cell.tag = indexPath.row+1;
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    return 300;
}


#pragma mark ----刷新请求数据
-(void)TapBarButton:(id)sender{


    [DownLoadData getRequestType:Enum_MusicPage withData:^(id obj, NSError *err) {
        if(obj)
        {
            [self.dataArr removeAllObjects];
            self.dataArr=obj;
            
            [self.tableView reloadData];
            self.tableView.contentOffset = CGPointMake(0, 0);
            
        }
        else
        {
            UIAlertView * a = [[UIAlertView alloc] initWithTitle:@"提示" message:@"数据下载失败" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [a show];
            NSLog(@"段子---数据下载失败");
            a = nil;
        }
        
    } withPage:0];



}

#pragma mark----上拉刷新
-(void)loadNewData{
    
    
    [DownLoadData getRequestType:Enum_MusicPage withData:^(id obj, NSError *err) {
        if(obj)
        {
            
            self.dataArr=obj;
            
            [self.tableView reloadData];
            [self.tableView.footer endRefreshing];
        }
        else
        {
            UIAlertView * a = [[UIAlertView alloc] initWithTitle:@"提示" message:@"数据下载失败" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [a show];
            NSLog(@"段子---数据下载失败");
            a = nil;
        }
        
    } withPage:0];
    [self.tableView.header endRefreshing];
}

#pragma mark---下拉加载
-(void)loadMoreData{
    
    static int page=1;
    [ DownLoadData getRequestType:Enum_MusicPage withData:^(id obj, NSError *err) {
        if(obj)
        {
            
            [self.dataArr addObjectsFromArray:obj];
            
            [self.tableView reloadData];
            [self.tableView.footer endRefreshing];
        }
        else
        {
            UIAlertView * a = [[UIAlertView alloc] initWithTitle:@"提示" message:@"数据下载失败" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [a show];
            NSLog(@"段子---数据下载失败");
            a = nil;
        }

    } withPage:page++];
    
    [self.tableView.footer endRefreshing];
}

#pragma mark----cell下面的点击事件
-(void)choseTerm:(UIButton *)sender{
    
    if (!_audioStream) {
        NSURL *url=[NSURL URLWithString:@"http://wvoice.spriteapp.cn/voice/2015/0814/55cdcb0b5ca7d.mp3"];
        //创建FSAudioStream对象
        _audioStream=[[FSAudioStream alloc]initWithUrl:url];
        _audioStream.onFailure=^(FSAudioStreamError error,NSString *description){
            NSLog(@"播放过程中发生错误，错误信息：%@",description);
        };
        [_audioStream setVolume:1];//设置声音
    }

    
        [_audioStream pause];
        [_audioStream play];
    

    

}

-(void)DidSelectLoveButton:(UIButton *)sender{
    
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:@"谢谢您的赞👍" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
    [alert show];
    alert = nil;
    
    
    
}
-(void)DidSelectHateButton:(UIButton *)sender{
    
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:@"您好狠心啊😒" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
    [alert show];
    alert = nil;
    
    
}
-(void)DidSelectShareButton:(UIButton *)sender{
    
    
    
}




@end
